﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Entity;
using System.Data;

namespace Data
{
    public class Customer_Data

    {
        SqlConnection con;
        SqlCommand comn;
        SqlDataAdapter da;
        DataSet ds;

        public bool Insert_Customer(Customer_Entity customer)
        {
            try
            {
                con = Connection.StartConnection();

                comn = new SqlCommand("customerAdd", con);
                con.Open();
                comn.CommandType = CommandType.StoredProcedure;
                comn.Parameters.Add(new SqlParameter("@username", customer.Username));
                comn.Parameters.Add(new SqlParameter("@password", customer.Password));
                comn.Parameters.Add(new SqlParameter("@name", customer.Name));
                comn.Parameters.Add(new SqlParameter("@address", customer.Address));
                comn.Parameters.Add(new SqlParameter("@phone", customer.Phone));
                comn.Parameters.Add(new SqlParameter("@totalorders", customer.Totalorders));
                comn.Parameters.Add(new SqlParameter("@totalprice", customer.Totalprice));



                comn.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Connection.CloseConnection();
            }

            return true;
        }
        public DataTable Load()
        {
            con = Connection.StartConnection();
            comn = new SqlCommand("BindCus", con);
            comn.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(comn);

            ds = new DataSet();
            try
            {
                da.Fill(ds, "customer");
                return ds.Tables["customer"];
            }
            catch
            {
                throw;
            }
            finally
            {
                ds.Dispose();
                da.Dispose();
                con.Close();
                con.Dispose();
            }
        }
        public int Update_cus(int cusID, string username, string password, string name, string address, int phone, int totalorders, int totalprice)
        {
            con = Connection.StartConnection();
            con.Open();
            comn = new SqlCommand("UpdateCus", con);
            comn.CommandType = CommandType.StoredProcedure;
            try
            {
                comn.Parameters.AddWithValue("@cusID", cusID);
                comn.Parameters.AddWithValue("@username", username);
                comn.Parameters.AddWithValue("@password", password);
                comn.Parameters.AddWithValue("@name", name);
                comn.Parameters.AddWithValue("@address", address);
                comn.Parameters.AddWithValue("@phone", phone);
                comn.Parameters.AddWithValue("@totalorders", totalorders);
                comn.Parameters.AddWithValue("@totalprice", totalprice);
                return comn.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                Connection.CloseConnection();
            }

        }
        public int Delete(int personID)
        {
            con = Connection.StartConnection();
            con.Open();
            comn = new SqlCommand("DeleteCus", con);
            comn.CommandType = CommandType.StoredProcedure;
            try
            {
                comn.Parameters.AddWithValue("@cusID", personID);
                return comn.ExecuteNonQuery();
            }
            catch
            {
                throw;
            }
            finally
            {
                comn.Dispose();
                con.Close();
                con.Dispose();
            }
        }
    }

}
